layui.config({
	base: '../lay/system/'
}).use(['layer', 'element', 'edit', 'flow'], function() {
	var element = layui.element,
		layer = layui.layer,
		$ = layui.$,
		edit = layui.edit,
		flow = layui.flow;
	//建立编辑器
	edit.layEditor({
		elem: '.article_editor'
	});
	$(function() {
		//提交回复
		$('.layui-submit').click(function() {
			//token，cookie验证登录
			var data = {};
			data.uid = $.trim($('#uid').val());
			var content = $.trim($('.article_editor').val());
			if(content.length < 5) {
				layer.msg('回复不能小于5个非空格字符', {
					icon: 2,
					time: 2000
				});
				return flase;
			}
			//内容转义
			content = edit.content(content);
			data.content = content;
			$.ajax({
				url: "http://127.0.0.1:8020",
				data: JSON.stringify(data),
				success: function(result, status, xhr) {
					layer.closeAll('loading');
					layer.msg(result.data.msg, {
						icon: 2,
						time: 1000
					});
				}
			});
		})
		//加载更多
		flow.load({
			elem: '#comment_list',
			isAuto: false,
			done: function(page, next) {
				var lis = [];
				$.get('/json/comment.json?page=' + page, function(res) {
					layer.closeAll('loading');
					layui.each(res.data, function(index, item) {
						var html = "<li>" +
							"<a class='user_header_img'><img src='../images/my/wm/16.png'/></a>" +
							"<a class='user_nickname' title='作者' uid='002'>偶尔有点困</a>回复" +
							"<a class='user_nickname'>时光荏苒</a>:" +
							"<span class='comment_content'>" +
							"问我干嘛，这是新闻，问新华社去" +
							"</span>" +
							"<div class='info'>" +
							"<span>2018-4-24 13:33</span>" +
							"<a class='reply'>回复</a>" +
							"</div>" +
							"</li>";
						lis.push(html);
					});
					next(lis.join(''), page < res.pages);
				});
			}
		});

		//回复
		$('#comment_list').on('click', 'li .info .reply', function() {
			var u = $(this).parent('.info').siblings('.user_nickname').eq(0);
			var nick_name = u.attr('title');
			var text = '@' + nick_name + " ";
			$('#uid').val(u.attr('uid'))
			$('#article_editor').attr('placeholder', text)
			$('#article_editor').focus()
		})

	})
});